package com.woniuxy.service.impl;

import com.woniuxy.mapper.StudentDao;
import com.woniuxy.mapper.impl.StudentDaoImpl;
import com.woniuxy.entity.Student;
import com.woniuxy.exception.EmailExsitsException;
import com.woniuxy.exception.UserNameExsitsException;
import com.woniuxy.service.StudentService;

import java.util.List;

/**
 * @author 作者
 * @create 2025-06-11 14:14
 */
public class StudentServiceImpl implements StudentService {

    StudentDao studentDao=new StudentDaoImpl();

    @Override
    public void register(Student student) {
        //业务层实现学生注册，调用dao的insert()
        //同名用户只能存在一个
        if(studentDao.selectBySname(student.getSname())!=null){
            throw new UserNameExsitsException("用户名已经被使用");
        }
        if(studentDao.selectByEmail(student.getEmail())!=null){
            throw new EmailExsitsException("用户名已经被使用");
        }
        //同名邮箱只能注册一次
        String sid = studentDao.selectMaxSid();
        student.setSid(Integer.parseInt(sid)+1+"");
        studentDao.insertStudent(student);
    }

    @Override
    public Student login(String sname, String pwd) {
        Student student = studentDao.selectBySname(sname);
        if(student==null){
            throw  new RuntimeException("用户名不存在");
        }
        if(!pwd.equals(student.getPwd())){
            throw  new RuntimeException("密码错误");
        }
        return student;
    }

    @Override
    public List<Student> getAll() {
        return studentDao.selectAll();
    }
}
